﻿using Coldairarrow.Entity.Goods;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Goods
{
    public class BTB_GoodsSpecificationsItemBusiness : BaseBusiness<BTB_GoodsSpecificationsItem>, IBTB_GoodsSpecificationsItemBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_GoodsSpecificationsItemBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<BTB_GoodsSpecificationsItem> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(BTB_GoodsSpecificationsItem data)
        {
            BTB_GoodsSpecificationsItem data1 = new BTB_GoodsSpecificationsItem()
            {
                GoodsSpecificationsItemID = GuidHelper.GenerateKey(),
                CreatorId = _operator.UserId,
                GoodsID = data.CreatorId,
                Price = data.Price,
                Inventory = data.Inventory,
                CreateTime = data.CreateTime,
                Editor = data.Editor,
                EditTime = data.EditTime,
                Deleted = false,
                IsSale = data.IsSale,
            };
            await InsertAsync(data1);
        }

        public async Task UpdateDataAsync(BTB_GoodsSpecificationsItem data)
        {
            BTB_GoodsSpecificationsItem data1 = new BTB_GoodsSpecificationsItem()
            {
                GoodsSpecificationsItemID = data.GoodsSpecificationsItemID,
                CreatorId = data.CreatorId,
                GoodsID = data.CreatorId,
                Price = data.Price,
                Inventory = data.Inventory,
                CreateTime = data.CreateTime,
                Editor = _operator.UserId,
                EditTime = data.EditTime,
                Deleted = false,
                IsSale = data.IsSale,
            };
            await UpdateAsync(data1);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 采购管理
        /// <summary>
        /// 预警单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<BTB_GoodsSpecificationsItem>> GetGoodsList(PageInput<BTB_GoodsSpecificationsItem> input)
        {
            var bTB_GoodsTypeDto = input.Search;

            var result = Db.GetIQueryable<BTB_GoodsSpecificationsItem>().Where(z=>z.Inventory < z.stock);
            //品牌
            if (!string.IsNullOrEmpty(bTB_GoodsTypeDto.GoodsSpecifications))
            {
                result = result.Where(k => k.GoodsSpecifications.Contains(bTB_GoodsTypeDto.GoodsSpecifications));
            }
            //商品id
            if (!string.IsNullOrEmpty(bTB_GoodsTypeDto.Goodsname))
            {
                result = result.Where(a=>a.GoodsID == (Db.GetIQueryable<BTB_Goods>(true).Where(z=>z.GoodsName.Contains(bTB_GoodsTypeDto.Goodsname)).FirstOrDefault().ID));
            }
            var where = LinqHelper.True<BTB_GoodsSpecificationsItem>();

            int count = result.Count();
            var q = await result.Select(s => new BTB_GoodsSpecificationsItem
            {
                GoodsSpecificationsItemID = s.GoodsSpecificationsItemID,
                CreatorId = s.CreatorId,
                GoodsID = Db.GetIQueryable<BTB_Goods>(true).Where(g => g.ID == s.GoodsID).FirstOrDefault().GoodsName,
                Price = s.Price,
                Inventory = s.Inventory,
                GoodsSpecifications = s.GoodsSpecifications,
                CreateTime = s.CreateTime,
                Editor = s.Editor,
                EditTime = s.EditTime,
                Deleted = s.Deleted,
                IsSale = s.IsSale,
                stock = s.stock,
                LackNumber = s.stock - s.Inventory
            }).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_GoodsSpecificationsItem> { Data = q, Total = count };
        }
        #endregion
    }
}